//package com.ashlikun.wheelview_rv
//
//import android.content.Context
//import android.graphics.Color
//import android.util.AttributeSet
//import android.util.Log
//import android.view.ViewGroup
//import androidx.recyclerview.widget.LinearLayoutManager
//import androidx.recyclerview.widget.LinearSnapHelper
//import androidx.recyclerview.widget.RecyclerView
//import com.ashlikun.wheelview_rv.WheelUtils.createLayoutParams
//
///**
// * @author　　: 李坤
// * 创建时间: 2024/7/23 13:44
// * 邮箱　　：496546144@qq.com
// *
// * 功能介绍：
// */
//class WheelView : ViewGroup {
//    private var mRecyclerView: WheelRecyclerView? = null
//
//    /**
//     * 分割线颜色
//     */
//    private var dividerColor = Color.BLACK
//
//    /**
//     * item数量
//     */
//    private var offsetCount = 3
//
//    /**
//     * item大小
//     */
//    private var itemSize = 90
//
//    /**
//     * 分割线之间距离
//     */
//    private var dividerSize = 90
//
//    /**
//     * 布局方向
//     */
//    private var orientation = WHEEL_VERTICAL
//
//    /**
//     * 对齐方式
//     */
//    private var gravity = WheelRecyclerView.GRAVITY_CENTER
//    private var lastSelectedPosition = IDLE_POSITION
//    private var selectedPosition = IDLE_POSITION
//    internal var wheelAdapter: WheelWrapAdapter? = null
//        private set
//
//    constructor(context: Context) : super(context) {
//        init(context, null)
//    }
//
//    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
//        init(context, attrs)
//    }
//
//    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
//        init(context, attrs)
//    }
//
//    private fun init(context: Context, attrs: AttributeSet?) {
//        if (attrs != null) {
//            val a = context.obtainStyledAttributes(attrs, R.styleable.WheelView)
//            offsetCount = a.getInt(R.styleable.WheelView_wheelItemCount, offsetCount)
//            dividerColor = a.getColor(R.styleable.WheelView_dividerColor, dividerColor)
//            itemSize = a.getDimensionPixelOffset(R.styleable.WheelView_wheelItemSize, itemSize)
//            dividerSize = a.getDimensionPixelOffset(R.styleable.WheelView_wheelDividerSize, dividerSize)
//            orientation = a.getInt(R.styleable.WheelView_wheelOrientation, orientation)
//            gravity = a.getInt(R.styleable.WheelView_wheelGravity, gravity)
//            a.recycle()
//        }
//        initRecyclerView(context)
//    }
//
//    private fun initRecyclerView(context: Context) {
//        mRecyclerView = WheelRecyclerView(context, gravity, offsetCount, itemSize, orientation, dividerColor, dividerSize)
//        mRecyclerView!!.overScrollMode = OVER_SCROLL_NEVER
//        val totolItemSize = (offsetCount * 2 + 1) * itemSize
//        //让滑动结束时都能定到中心位置
//        LinearSnapHelper().attachToRecyclerView(mRecyclerView)
//        this.addView(mRecyclerView, createLayoutParams(orientation, totolItemSize))
//        mRecyclerView!!.addOnScrollListener(object : RecyclerView.OnScrollListener() {
//            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
//                if (listener == null || newState != RecyclerView.SCROLL_STATE_IDLE || wheelAdapter == null) return
//                val centerItemPosition = wheelAdapter!!.getRealPosition(mRecyclerView!!.findCenterItemPosition())
//                if (centerItemPosition == IDLE_POSITION) return
//                selectedPosition = centerItemPosition
//                if (selectedPosition != lastSelectedPosition) {
//                    listener!!.onItemSelected(centerItemPosition)
//                    lastSelectedPosition = selectedPosition
//                }
//            }
//        })
//    }
//
//
//
//    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
//        if (childCount <= 0) {
//            return
//        }
//        val child = getChildAt(0)
//        val childWidth = child.measuredWidth
//        val childHeight = child.measuredHeight
//        val left: Int
//        val top: Int
//        if (orientation == WHEEL_HORIZONTAL) { //水平布局时,最好固定高度,垂直布局时最好固定宽度
//            val centerWidth = width - paddingLeft - paddingRight - childWidth shr 1
//            left = paddingLeft + centerWidth
//            top = paddingTop
//        } else {
//            val centerHeight = height - paddingTop - paddingBottom - childHeight shr 1
//            left = paddingLeft
//            top = paddingTop + centerHeight
//        }
//        child.layout(left, top, left + childWidth, top + childHeight)
//    }
//
//    private var listener: OnItemSelectedListener? = null
//    fun setOnItemSelectedListener(listener: OnItemSelectedListener) {
//        this.listener = listener
//    }
//
//    /**
//     * item selected
//     */
//    interface OnItemSelectedListener {
//        fun onItemSelected(index: Int)
//    }
//
//    fun setAdapter(adapter: RecyclerView.Adapter<out RecyclerView.ViewHolder>) {
//        if (adapter == null) {
//            mRecyclerView!!.adapter = null
//            return
//        }
//        wheelAdapter = WheelWrapAdapter(adapter, orientation, itemSize, offsetCount)
//        mRecyclerView!!.adapter = wheelAdapter
//
//    }
//
//    companion object {
//        /**
//         * 无效的位置
//         */
//        const val IDLE_POSITION = -1
//
//        /**
//         * 垂直与水平布局两种状态
//         */
//        const val WHEEL_VERTICAL = LinearLayoutManager.VERTICAL
//        const val WHEEL_HORIZONTAL = LinearLayoutManager.HORIZONTAL
//    }
//}
